print("Loaded class GoSRE.Groovy")

def go_build_prerequisite(String target, /*Boolean*/ String useLocalProxy) {
    sh '''
        mkdir -p /go/src/$target/
        ln -s ${PWD} /go/src/$target
        echo "Current JOB: ${JOB_NAME}" 
        cd /go/src/$target/${JOB_NAME}/src
        ls -la .
        go env -w GOSUMDB=off
        go env -w GO111MODULE=on
        if [ $useLocalProxy = "true" ]; then
            go env -w GOPROXY=https://jinquan:passw0rd@idc-nexus.supor.com/repository/group-idc-golang-proxy/,direct
        else
            go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct
        fi
    '''
}

def docker_build(String dockerfile, String credential , String registry, String project, 
                    String branch, String app_routine_name, String app_version) {
    withCredentials([usernamePassword(credentialsId: "${credential}", passwordVariable: 'Pass', usernameVariable: 'User')]) {
        container('docker') {
            // Pass function's parameters to container inside environment variables
            withEnv(["Registry=${registry}", "Repository=${project}", "dockerFile=${dockerfile}", "Branch=${branch}",
                    "AppRoutine=${app_routine_name}", "AppVersion=${app_version}"]) {
                sh '''
                    df -h
                    ls -la /var/run/        
                    echo $Registry/$Repository
                    docker login -u$User -p$Pass $Registry
                '''
                sh '''
                    ## root directory: src
                    cd src
                    ls -la .
                    export DOCKER_IMAGE=$Registry/$Repository:$(cat ${AppVersion})"-"${Branch}"-build"${BUILD_NUMBER}
                    # docker build -f $dockerFile -t $DOCKER_IMAGE .
                    docker build --build-arg APP_ROUTINE="${AppRoutine}" -t $DOCKER_IMAGE -f $dockerFile .
                    docker push $DOCKER_IMAGE
                '''
            }
        }
    }
}

def deploy_app(String apiserver, String credential, String namespace, 
                String ingress_yaml, String ingress_tls_secret, String ingress_domain, 
                String deploy_yaml, String k8sapp_name, String configmap_yaml, 
                String registry, String project, String branch, String version) {
    container('kubectl') {
        // Pass function's parameters to container inside environment variables
        withEnv(["Namespace=${namespace}", 
                "Ingress=${ingress_yaml}", "IngressTlsSecret=${ingress_tls_secret}", "IngressDomain=${ingress_domain}",
                "Deployment=${deploy_yaml}", "App=${k8sapp_name}", "AppConfigmap=${configmap_yaml}", 
                "Registry=${registry}", "Repository=${project}", "Branch=${branch}", "Version=${version}"]) {
            withKubeConfig([credentialsId: "${credential}", serverUrl: "${apiserver}"]) {
                sh '''
                    ## root directory: src
                    cd src
                    ls -la .
                    export DOCKER_IMAGE=$Registry/$Repository:$(cat ${Version})"-"${Branch}"-build"${BUILD_NUMBER}
                    export NAMESPACE=${Namespace}
                    export APP_NAME=${App}
                    export INGRESS_DOMAIN=${IngressDomain}
                    export INGRESS_TLS_SECRET=${IngressTlsSecret}
                    echo $DOCKER_IMAGE,$NAMESPACE,$APP_NAME
                    if [ -n "${AppConfigmap}" ]; then
                        envsubst < ${AppConfigmap} | kubectl apply -f -
                    fi
                    if [ -n "${Ingress}" ]; then
                        envsubst < ${Ingress} | kubectl apply -f -
                    fi
                    envsubst < ${Deployment} | kubectl apply -f -
                '''
            }
        }
    }
}

def send_mail(String recipient) {
    println("send mail to: " + recipient)
        emailext attachLog: true, compressLog: true,
        subject: '$DEFAULT_SUBJECT',
        body: '$DEFAULT_CONTENT',
        to: "${recipient}"
}

return this